﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LaughingDog.GameObjects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using FarseerGames.FarseerPhysics.Collisions;

namespace LaughingDog.Components.Shapes
{
    public class PolygonShape : Shape
    {
        public PolygonShape(GameObject gameObject)
            : base(gameObject)
        {
        }

        /// <summary>
        /// Joins the last Vector2 in the Polygon to the first one.
        /// </summary>
        public void Join()
        {
            if(Vectors.Count > 0)
                AddVector(new Vector2(Vectors[0].X, Vectors[0].Y));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="vectors"></param>
        public void SetVectors(List<Vector2> vectors)
        {
            ClearVectors();

            foreach (Vector2 vector in vectors)
                AddVector(vector);
        }

        /// <summary>
        /// 
        /// </summary>
        public void CreateFromTexture()
        {
            if (Renderer != null)
                if (Renderer.Texture != null)
                    CreateFromTexture(Renderer.Texture);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="texture"></param>
        public void CreateFromTexture(Texture2D texture)
        {
            ClearVectors();

            uint[] textureData = new uint[texture.Width * texture.Height];
            texture.GetData(textureData);

            Vertices verts = Vertices.CreatePolygon(textureData, texture.Width, texture.Height);

            SetVectors(verts);

            Join();
        }
    }
}
